Skip to content

[Refactor] OpenAI 클라이언트 공통 호출 레이어 추출#154

Merged
marshmallowing merged 2 commits intodevelopfrom
feature/149
Feb 19, 2026
Merged

[Refactor] OpenAI 클라이언트 공통 호출 레이어 추출#154
marshmallowing merged 2 commits intodevelopfrom
feature/149

Conversation

@marshmallowing
Copy link
Copy Markdown
Member

@marshmallowing marshmallowing commented Feb 19, 2026

🔍️ 작업 내용

  • Closes #

✨ 상세 설명

OpenAI를 호출하는 방식이 서비스마다 달라 유지보수가 어려운 문제를 개선했습니다.
기존 문제

서비스 기존 방식
AiSearchService RestTemplate 직접 HTTP 호출 + 커스텀 DTO
AiMaterialService 생성자에서 new OpenAiService() 직접 생성
AiReviewGeneratorService @bean 주입

같은 OpenAI API를 호출하는데 클라이언트가 사실상 3개였고,
에러 처리와 로깅도 각 서비스마다 중복 구현되어 있었습니다.

해결
global/openai 패키지에 공통 레이어를 추가하고 3개 서비스를 일원화했습니다.

  • OpenAiClient : API 호출 / 에러 처리 / 소요시간 로깅을 한 곳에서 관리
  • OpenAiRequest : model, systemPrompt, userMessage, temperature를 묶는 값 객체 (record)
  • OpenAiException : AI 호출 실패 전용 예외, 서비스별 fallback 처리 가능

🛠️ 추후 리팩토링 및 고도화 계획

📸 스크린샷 (선택)

image

💬 리뷰 요구사항

Summary by CodeRabbit

릴리스 노트

  • 버그 수정

    • AI 기반 기능의 오류 처리 개선으로 안정성 강화
    • 검색 키워드 추출 시 예외 발생 시 대체 경로 추가
  • 개선 사항

    • AI 생성 콘텐츠의 신뢰성 및 품질 향상
    • 제품 자료, 리뷰, 검색 기능의 일관된 오류 처리 메커니즘 적용

@marshmallowing marshmallowing self-assigned this Feb 19, 2026
@marshmallowing marshmallowing added the ♻️ Refactor 리팩토링 label Feb 19, 2026
@marshmallowing marshmallowing merged commit a8af61c into develop Feb 19, 2026
1 check passed
@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai bot commented Feb 19, 2026

Caution

Review failed

The pull request is closed.

Walkthrough

OpenAI 통합을 새로운 OpenAiClient 중심의 아키텍처로 리팩토링했습니다. OpenAiRequest 레코드, OpenAiException, OpenAiClient 클래스를 신규 추가했으며, 세 개의 AI 서비스(AiMaterialService, AiReviewGeneratorService, AiSearchService)에서 이 새로운 클라이언트를 사용하도록 변경했습니다. SizeReviewPrompter의 프롬프트는 구조화된 단일 문장 생성 형식으로 전면 재작성되었습니다.

Changes

Cohort / File(s) Summary
OpenAI 기본 인프라
src/main/java/com/ongil/backend/global/openai/OpenAiClient.java, OpenAiException.java, OpenAiRequest.java
새로운 OpenAiClient 컴포넌트와 예외 처리, 요청 데이터 구조 추가. OpenAiClient는 OpenAiService를 감싸면서 시간 측정 및 로깅을 제공합니다.
AI 서비스 리팩토링
src/main/java/com/ongil/backend/domain/product/service/AiMaterialService.java, src/main/java/com/ongil/backend/domain/review/service/AiReviewGeneratorService.java, src/main/java/com/ongil/backend/domain/search/service/AiSearchService.java
OpenAiService 직접 호출을 OpenAiClient 기반 흐름으로 변경. RestTemplate 제거, OpenAiException 예외 처리 추가, Lombok 기반 의존성 주입 도입.
SizeReviewPrompter 프롬프트 개편
src/main/java/com/ongil/backend/domain/review/service/prompter/SizeReviewPrompter.java
기존의 규칙 중심 프롬프트를 의류 유형별 구체적 시나리오 기반의 구조화된 단일 문장 생성 방식으로 변경. 강도 표현, 상황별 문장, 출력 형식 규칙 명시.

Sequence Diagram

sequenceDiagram
    participant Service as AI Service
    participant OpenAiClient
    participant OpenAiService
    participant OpenAI API

    Service->>OpenAiClient: call(OpenAiRequest)
    activate OpenAiClient
    OpenAiClient->>OpenAiClient: construct ChatCompletionRequest
    OpenAiClient->>OpenAiService: call OpenAI API
    activate OpenAiService
    OpenAiService->>OpenAI API: POST /chat/completions
    activate OpenAI API
    OpenAI API-->>OpenAiService: response
    deactivate OpenAI API
    OpenAiService-->>OpenAiClient: ChatCompletionResponse
    deactivate OpenAiService
    alt Success
        OpenAiClient->>OpenAiClient: log execution time
        OpenAiClient-->>Service: extracted content
    else Exception
        OpenAiClient->>OpenAiClient: log error with duration
        OpenAiClient-->>Service: throw OpenAiException
    end
    deactivate OpenAiClient
Loading

Estimated code review effort

🎯 4 (Complex) | ⏱️ ~50 minutes

새로운 인프라 계층(OpenAiClient, OpenAiException, OpenAiRequest) 추가와 함께 세 개의 독립적인 서비스에서 서로 다른 패턴의 리팩토링이 발생합니다. 각 서비스별 예외 처리 방식, 프롬프트 구성 로직, 응답 파싱이 다르므로 개별 검토가 필요하며, SizeReviewPrompter의 프롬프트 변경사항도 의도와 정확성을 확인해야 합니다.

Possibly related PRs

✨ Finishing Touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch feature/149

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

♻️ Refactor 리팩토링

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant